<template>
  <el-dialog
    title="添加员工"
    :visible.sync="isDialog"
    width="30%"
    :before-close="handleClose"
  >
    <!--  表单 -->
    <el-form label-width="120px" :rules="rules" ref="liu" :model="formData">
      <el-form-item label="姓名" prop="username">
        <el-input
          style="width: 80%"
          v-model="formData.username"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          style="width: 80%"
          v-model="formData.mobile"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          type="date"
          style="width: 80%"
          placeholder="请选择入职时间"
          v-model="formData.timeOfEntry"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          style="width: 80%"
          v-model="formData.formOfEmployment"
          placeholder="请选择"
        >
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          style="width: 80%"
          v-model="formData.workNumber"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          style="width: 80%"
          v-model="formData.departmentName"
          placeholder="请选择部门"
          @focus="getShuList"
        />
        <el-tree
          v-loading="loading"
          v-if="isTree"
          :data="dateList"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          style="width: 80%"
          v-model="formData.correctionTime"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="addYuan">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartments } from "@/api/departments";
import { addEmployeed } from "@/api/employees";
import { departmentData } from "@/utils";
import EmployeeEnum from "@/api/constant/employees";
export default {
  props: {
    isDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
  },
  created() {},
  data() {
    return {
      // 在 外部导入的 聘用形式
      EmployeeEnum,
      // 表单双向绑定
      formData: {
        username: "111",
        mobile: "18336110000",
        formOfEmployment: "",
        workNumber: "1",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      isTree: false, // 树形控件是否展示
      loading: false, // 加载动画
      dateList: [], // 树形控件
      // 属性控件展示格式
      defaultProps: {
        label: "name",
      },
      rules: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 关闭弹出层事件
    handleClose() {
      this.$refs.liu.resetFields();
      this.closeDialog();
    },
    // 关闭弹出层
    closeDialog() {
      this.$emit("update:isDialog", false);
    },
    // 获取部门的数据
    async getShuList() {
      this.loading = true;
      const data = await getDepartments();
      this.dateList = departmentData(data.depts, "");
      this.isTree = true;
      this.loading = false;
    },
    // 选中属性控件的回调
    handleNodeClick(val) {
      this.formData.departmentName = val.name;
      this.isTree = false;
    },
    // 添加员工
    async addYuan() {
      try {
        await this.$refs.liu.validate();
        const data = await addEmployeed(this.formData);
        this.$message({
          message: "添加成功",
          type: "success",
        });
        // 调用父组件方法刷新
        this.$parent.getList();
        this.closeDialog();
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
</style>
